﻿using System;
using System.Collections.Generic;
using KPIS.DBM;
using System.Configuration;
using System.Data;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.DAL
{
    public class AssetDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public IList<AssetInfo> GetAsset(Nullable<int> Sequence, string SearchText)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<AssetInfo> infoList = new List<AssetInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@SearchText", SearchText);
                dbManager.AddParameters(2, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = " SELECT ass.*, rc.icm_receive_code, rc.receive_date "
                        + " FROM ICM_ASSET ass "
                        + " LEFT JOIN ICM_RECEIVE rc ON ass.ICM_RECEIVE_SEQ = rc.ICM_RECEIVE_SEQ "
                        + " AND rc.RECORD_STATUS = @ActiveRecord "
                        + " WHERE ass.RECORD_STATUS = @ActiveRecord ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_IcmReceiveObject reInfo = new M_IcmReceiveObject();
                        AssetInfo info = new AssetInfo();
                        info.Sequence = Convert.ToInt32(drc[i]["asset_seq"]);
                        info.Code = drc[i]["asset_code"].ToString();
                        info.Name = drc[i]["asset_name"].ToString();
                        info.AssetDate = Convert.ToDateTime(drc[i]["asset_date"]);
                        info.SerialNumber = drc[i]["serial_number"].ToString();
                        info.Price = Convert.ToDouble(drc[i]["price"].ToString());
                        info.Remarks = drc[i]["asset_remark"].ToString();
                        reInfo.icm_receive_seq = Convert.ToInt32(drc[i]["icm_receive_seq"]);
                        reInfo.icm_receive_code = drc[i]["icm_receive_code"].ToString();
                        reInfo.receive_date = cn.ConvertNullDateTime(drc[i]["receive_date"]);

                        info.ReceiveInfo = reInfo;

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public AssetInfo GenerateAssetCode(string asset_code)
        {
            string strSQL;
            DataRowCollection drc = null;
            AssetInfo info = new AssetInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@asset_code", asset_code);

                //strSQL = " SELECT case when  (Year(Now()) <2500) then Year(Now())+543 else Year(Now()) end YearValue , Month(Now()) as MonthValue  ,COUNT(asset_code)+1 as running "
                //         +" FROM icm_asset asset "
                //         +" WHERE YEAR(asset_date) = YEAR(Now()) and "
                //         +" Month(asset_date) = Month(Now())  ";
                strSQL = " SELECT case when  (Year(Now()) <2500) then Year(Now())+543 else Year(Now()) end YearValue , Month(Now()) as MonthValue  "
                        + " ,COUNT(asset_code)+1 as running "
                        + " FROM icm_asset asset  "
                        + " WHERE YEAR(created_when) = YEAR(Now()) and  Month(created_when) = Month(Now())  and (right(left(asset_code,19),12) = @asset_code) ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.stryear = drc[0]["YearValue"].ToString();
                    info.strmonth = drc[0]["MonthValue"].ToString();
                    info.CodeRunning = drc[0]["running"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public AssetInfo GenerateRunningNumber(string sub_asset_code)
        {
            string strSQL;
            DataRowCollection drc = null;
            AssetInfo info = new AssetInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@sub_asset_code", sub_asset_code);

                strSQL = " SELECT count(asset_code)+1 as running  "
                        + " FROM icm_asset "
                        + " WHERE record_status <>'D' "
                        + " and left(asset_code,19) =@sub_asset_code  ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.CodeRunning = drc[0]["running"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(AssetInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(12);
                dbManager.AddParameters(0, "@asset_code", info.Code);
                dbManager.AddParameters(1, "@asset_name", info.Name);
                dbManager.AddParameters(2, "@asset_date", info.AssetDate);
                dbManager.AddParameters(3, "@serial_number", info.SerialNumber);
                dbManager.AddParameters(4, "@asset_remark", info.Remarks);
                dbManager.AddParameters(5, "@depreciate_below_flag", info.DepreciateBelowFlag);
                dbManager.AddParameters(6, "@price", info.Price);
                dbManager.AddParameters(7, "@icm_res_item_seq", info.ResourceItemInfo == null ? null : info.ResourceItemInfo.Seq);
                dbManager.AddParameters(8, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(9, "@created_by", info.Created_by);
                dbManager.AddParameters(10, "@created_when", DateTime.Today);
                dbManager.AddParameters(11, "@icm_receive_seq", info.ReceiveInfo == null ? 0 : info.ReceiveInfo.icm_receive_seq);

                strSQL = " INSERT INTO ICM_ASSET "
                        + " (ASSET_CODE,ASSET_NAME,ASSET_DATE,SERIAL_NUMBER,ASSET_REMARK "
                        + " ,DEPRECIATE_BELOW_FLAG,PRICE,ICM_RES_ITEM_SEQ,RECORD_STATUS "
                        + " ,CREATED_BY,CREATED_WHEN,ICM_RECEIVE_SEQ) "
                        + " VALUES "
                        + " (@asset_code,@asset_name,@asset_date,@serial_number,@asset_remark "
                        + " ,@depreciate_below_flag,@price,@icm_res_item_seq,@record_status "
                        + " ,@created_by,@created_when,@icm_receive_seq) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(AssetInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(11);
                dbManager.AddParameters(0, "@asset_code", info.Code);
                dbManager.AddParameters(1, "@asset_name", info.Name);
                dbManager.AddParameters(2, "@asset_date", info.AssetDate);
                dbManager.AddParameters(3, "@serial_number", info.SerialNumber);
                dbManager.AddParameters(4, "@asset_remark", info.ReceiveInfo);
                dbManager.AddParameters(5, "@depreciate_below_flag", info.DepreciateBelowFlag);
                dbManager.AddParameters(6, "@price", info.Price);
                dbManager.AddParameters(7, "@icm_res_item_seq", info.ResourceItemInfo == null ? null : info.ResourceItemInfo.Seq);
                dbManager.AddParameters(8, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(9, "@updated_by", info.Updated_by);
                dbManager.AddParameters(10, "@updated_when", DateTime.Today);

                strSQL = " UPDATE icm_asset "
                        + " SET asset_name = @asset_name "
                        + " ,asset_date = @asset_date "
                        + " ,serial_number = @serial_number "
                        + " ,asset_remark =@asset_remark "
                        + " ,depreciate_below_flag = @depreciate_below_flag "
                        + " ,price = @price "
                        + " ,icm_res_item_seq = @icm_res_item_seq "
                        + " ,record_status = @record_status "
                        + " ,updated_by =@updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE asset_code = @asset_code ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }


        public void UpdateIcmReceive(M_IcmReceiveObject info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);
                dbManager.AddParameters(0, "@asset_code_start", info.asset_code_start);
                dbManager.AddParameters(1, "@asset_code_finish", info.asset_code_finish);
                dbManager.AddParameters(2, "@icm_receive_seq", info.icm_receive_seq);

                strSQL = " UPDATE ICM_ASSET "
                        + " SET ICM_RECEIVE_SEQ = @icm_receive_seq "
                        + " WHERE ASSET_CODE >=  '@asset_code_start' "
                        + " AND ASSET_CODE <= '@asset_code_finish' ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }


        public void Delete(string asset_code_start, string asset_code_finish, string remark)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);
                dbManager.AddParameters(0, "@asset_code_start", asset_code_start);
                dbManager.AddParameters(1, "@asset_code_finish", asset_code_finish);
                dbManager.AddParameters(2, "@remark", remark);
                dbManager.AddParameters(3, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = " UPDATE ICM_ASSET "
                        + " SET RECORD_STATUS = @DeleteRecord "
                        + " , ASSET_REMARK = @remark "
                        + " WHERE ASSET_CODE >= '@asset_code_start' "
                        + " AND ASSET_CODE <= '@asset_code_finish' ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
